package minefak3r;

/**
 *
 * @author Chriss & D4rky!
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Random;
import static minefak3r.Packages.*;

public class PackageManipulator {

    static Random ran = new Random();

    public static void handleLoginRequest(DataInputStream dis, DataOutputStream dos) throws Exception {
        dos.writeByte(PKG_LOGINREQUEST);
        Player.changeID(dis.readInt());
        dos.writeInt(Player.getID());
        int bytes = dis.available();
        byte data[] = new byte[bytes];
        dis.readFully(data);
        dos.write(data);
    }

    public static void handleOnGround(DataInputStream dis, DataOutputStream dos) throws Exception {
        dis.readBoolean();
        dos.writeByte(PKG_PLAYER);
        dos.writeBoolean(true);
    }

    public static void handlePosition(DataInputStream dis, DataOutputStream dos) throws Exception {
        dos.writeByte(PKG_PLAYERPOSITION);
        double faktor = 0;
        if (Player.getDrunk()) {
            faktor = ran.nextInt(5);
        }
        Player.changeX(dis.readDouble() + faktor);
        Player.changeY(dis.readDouble());
        Player.changeStance(dis.readDouble());
        Player.changeZ(dis.readDouble());
        dos.writeDouble(Player.getX());
        dos.writeDouble(Player.getY());
        dos.writeDouble(Player.getStance());
        dos.writeDouble(Player.getZ());
        dis.readBoolean();
        dos.writeBoolean(true);
    }

    public static void handlePositionLook(DataInputStream dis, DataOutputStream dos) throws Exception {
        dos.writeByte(PKG_PLAYERPOSLOOK);
        double faktor = 0;
        if (Player.getDrunk()) {
            faktor = ran.nextInt(5);
        }
        Player.changeX(dis.readDouble() + faktor);
        Player.changeY(dis.readDouble());
        Player.changeStance(dis.readDouble());
        Player.changeZ(dis.readDouble());
        Player.changeRotationX(dis.readFloat());
        Player.changeRotationY(dis.readFloat());
        dos.writeDouble(Player.getX());
        dos.writeDouble(Player.getY());
        dos.writeDouble(Player.getStance());
        dos.writeDouble(Player.getZ());
        dos.writeFloat(Player.getRotationX());
        dos.writeFloat(Player.getRotationY());
        dis.readBoolean();
        dos.writeBoolean(true);
    }

    public static void handleLook(DataInputStream dis, DataOutputStream dos) throws Exception {
        dos.writeByte(PKG_PLAYERLOOK);
        Player.changeRotationX(dis.readFloat());
        Player.changeRotationY(dis.readFloat());
        dos.writeFloat(Player.getRotationX());
        dos.writeFloat(Player.getRotationY());
        dis.readBoolean();
        dos.writeBoolean(true);
    }

    public static void handleHealth(DataInputStream dis, DataOutputStream dos) throws Exception {
        dos.writeByte(PKG_UPDATEHEALTH);
        int bytes = dis.available();
        byte data[] = new byte[bytes];
        System.out.println(bytes);
        dos.write(data);
    }
}
